/*
 * @Author: 张小平
 * @Date:   2025-05-01
 * @Email:  qqshuqian@163.com
 * @Copyright © 2024 aseyun. All rights reserved.
 */
import router from './router/'
import store from './store'

/**
 * TODO 搞复杂了，后续优化
 */
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import Store from "./router";
NProgress.configure({ showSpinner: false });
const lockPage = '/lock'; //锁屏页
router.beforeEach((to, from, next) => {
  const meta = to.meta || {};
  // to.name = to.name||"欢迎";
  const isMenu = meta.menu === undefined ? to.query.menu : meta.menu;
  //store.commit('SET_IS_MENU', isMenu === undefined);
  if (store.getters.token) {
    if (store.getters.isLock && to.path != lockPage) { //如果系统激活锁屏，全部跳转到锁屏页
      next({ path: lockPage })
    } else if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
      next({ path: '/' })
    } else {
      //如果用户信息为空则获取用户信息，获取用户信息失败，跳转到登录页
      if (2!=store.getters.refreshFlg) {
        store.dispatch('GetUserInfo').then(() => {
          // 立即执行动态路由
          router.$avueRouter.formatRoutes(store.getters.menu, true);
          next({ ...to, replace: true })

        }).catch((err) => {
          store.commit('SET_TOKEN', "")
          if (meta.isAuth === false) {
            next('/500')
          }else{

            next({
              path: '/login',
              query: {redirect: to.fullPath} // 将要跳转路由的path作为参数，传递到登录页面
            })
          }

          //alert(err)
         // router.push({ path: "/500"})

          // store.dispatch('FedLogOut').then(() => {
          //   next({ path: '/login' })
          // })
        })
      } else {
        const meta = to.meta || {}
        const query = to.query || {}
        if (meta.target) {
          window.open(query.url.replace(/#/g, "&"))
          return
        } else if (meta.isTab !== false) {
          store.commit('ADD_TAG', {
            name: query.name || to.name,
            path: to.path,
            fullPath: to.fullPath,
            params: to.params,
            query: to.query,
            meta: meta
          });
        }
        next()
      }
    }
  } else {

    if (to.path === '/login') { //如果登录成功访问登录页跳转到主页
      next()
    }else{
      //判断是否需要认证，
      if (!meta.isAuth) {
        if(store.getters.pubMenus===1){
          // 没有静态资源
          // TODO 没有静态资源不存在的也要这样处理
          // if(store.getters.menu.length==0 ){
          //   next({
          //     path: '/login',
          //     query: {redirect: to.fullPath} // 将要跳转路由的path作为参数，传递到登录页面
          //   })
          // }else{
          //   next()
          // }
          next();

        }else{

          store.dispatch('GetPubMenus').then(() => {
            router.$avueRouter.formatRoutes(store.getters.menu, true);
            next({ ...to, replace: true })
          }).catch((err) => {

          })
        }

      }
      else {
        next({
          path: '/login',
          query: {redirect: to.fullPath} // 将要跳转路由的path作为参数，传递到登录页面
        })
      }
    }

  }
})

router.afterEach(to => {
  NProgress.done();
  let title = router.$avueRouter.generateTitle(to)
  router.$avueRouter.setTitle(title);
  store.commit('SET_IS_SEARCH', false)
});
